/*******************************************************************************
 * Copyright (c) 2011 Erik Byström.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package se.slackers.automatunes.algorithm;

import java.util.HashSet;
import java.util.Set;

import se.slackers.automatunes.audio.Note;
import se.slackers.automatunes.audio.Playable;

/**
 * @author Erik Byström
 * 
 */
public abstract class BaseAudioRule implements AudioRule {
	private final Set<Note> notesOn = new HashSet<Note>();
	private final Set<Note> changeset = new HashSet<Note>();

	protected void noteOn(final Playable playable, final Note note) {
		if (notesOn.contains(note)) {
			notesOn.remove(note);
		} else {
			playable.noteOn(note);
		}
		changeset.add(note);
	}

	protected void turnOffOtherNotes(final Playable playable) {
		for (final Note note : notesOn) {
			playable.noteOff(note);
		}

		notesOn.clear();
		notesOn.addAll(changeset);
		changeset.clear();
	}
}
